<?php if (!defined("_ECRIRE_INC_VERSION")) return; if (class_exists('pdk_Logo')) return;
/**
 * SPIP Plugin Development Kit
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage pdk
 * @category   outils
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/Developmet-Kit
 * @version    1.0.0
 */

// ------------------------------------------------------------------------

/**
 *  pdk Logo Class
 * 
 * 	This class helps to manage logo images.
 *  When using this class don't forget that form must contain 'enctype="multipart/form-data"' attribute
 *
 * @package		plugins
 * @subpackage	pdk
 * @category	outils
 * @author		Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @link		http://www.spip-contrib.net/Developmet-Kit
 */
 //TODO: По идее надо убрать слеши после $this->img_dir (Например $this->img_dir.'/'.$this->filename.$id_logo.'.'.$ft;)
class pdk_Logo {
    
    /**
    * Images directory (wit '/' at the end!)
    */
    var $img_dir = _DIR_IMG;
    
    /**
    * Logo input name
    */
    var $logo_input_name = 'logo';
    
    /**
    * Logo filename - entity ID will be added to this name
    * Default: arton - filename for article's logo
    */
    var $filename = 'arton';
    
    /**
     *  Max file size
     */
    var $max_size = 2048;
    
    /**
	 * Constructor
	 */
	public function __construct()
	{
		
	}
    
    /**
     *  Upload logo
     * 
     *  @param int $id_logo Entity ID (for example id of article whose logo we are uploading)
     */
    function upload_logo($id_logo) {
        // Check upload directory:
        if(!file_exists($this->img_dir) or !is_dir($this->img_dir)) {
            mkdir($this->img_dir);
        }
    
    include_spip('classes/pdk_upload');
    
    $config = array();
    $config['upload_path'] = $this->img_dir;
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['max_size'] = $this->max_size;
    $config['encrypt_name'] = FALSE;
    $uploader = new pdk_Upload($config);
        
    if ($uploader->do_upload($this->logo_input_name)) {
        $file_data = $uploader->data();
        // If successfully uploaded the new image, then delete the old one and rename the new
        $this->delete_logo($id_logo);
        rename($this->img_dir.'/'.$file_data['file_name'], $this->img_dir.'/'.$this->filename.$id_logo.$file_data['file_ext']);        
    } else return false;
    
    return true;
    }
    
    /**
     *  Delete logo file
     */ 
    function delete_logo($id_logo) {
        // Перебираем все возможные варианты типов старого изображения
        $file_types = array('gif', 'jpg', 'png', 'jpeg');
            foreach ($file_types as $ft) {
                if (file_exists($this->img_dir.'/'.$this->filename.$id_logo.'.'.$ft))
                    if (!unlink($this->img_dir.'/'.$this->filename.$id_logo.'.'.$ft))
                        return false;    
            }
        return true;    
    }
    
    /**
     *  Create <img> tag with logo
     */
    function logo_img($id_logo) {
        $img_code = '';
        // Ищем картинку перебирая все возможные варианты типов. 
        // Выбрана будет первая найденная (хотя она и должна быть одна)
        $file_types = array('gif', 'jpg', 'png', 'jpeg');
        foreach ($file_types as $ft) {
            if (file_exists($this->img_dir.'/'.$this->filename.$id_logo.'.'.$ft)) {
                $filename = $this->img_dir.'/'.$this->filename.$id_logo.'.'.$ft;    
            } 
        }
        //if (!$filename)
        //    $filename = $this->img_dir.'/nophoto.png';
        if ($filename)
            $img_code = '<img src="'.$filename.'" />';
        return $img_code;
    }
}
?>